var asciiInputCount = 1;
var fileInputCount = 1;

const MAX_ASCII_INPUT = 3;
const MAX_FILE_INPUT = 3;

function addASCIIInput() {
    if (asciiInputCount <= MAX_ASCII_INPUT) {
        var asciiinput = document.getElementById("asciiinput");
        
        var element = document.createElement("div");
        element.setAttribute("id", "asciidiv" + asciiInputCount);
        
        var span = document.createElement("span");
        span.appendChild(document.createTextNode("Y"));
        var sub = document.createElement("sub");
        sub.textContent = asciiInputCount;
        span.appendChild(sub);
        span.appendChild(document.createTextNode(" = "));
        element.appendChild(span);
        
        var input = document.createElement("input");
        input.setAttribute("id", "asciiinput" + asciiInputCount);
        input.setAttribute("name", "ascii" + asciiInputCount);
        input.setAttribute("type", "text");
        input.setAttribute("onkeyup", "display(" + asciiInputCount + ", 'asciiinput" + asciiInputCount +"', 'asciioutput" + asciiInputCount + "');");
        element.appendChild(input);
        
        var img = document.createElement("img");
        img.setAttribute("src", "img/save.gif");
        img.setAttribute("alt", "Save");
        element.appendChild(img);
        
        var output = document.createElement("div");
        output.setAttribute("id", "asciioutput" + asciiInputCount);
        output.setAttribute("class", "mathml");
        element.appendChild(output);
        
        asciiinput.appendChild(element);
        asciiInputCount++;
    }
}

function addFileInput() {
    if (fileInputCount <= MAX_FILE_INPUT) {
        var fileinput = document.getElementById("fileinput");
        
        var element = document.createElement("div");
        element.setAttribute("id", "filediv" + fileInputCount);
        
        var input = document.createElement("input");
        input.setAttribute("id", "fileinput" + fileInputCount);
        input.setAttribute("name", "file" + fileInputCount);
        input.setAttribute("type", "file");
        input.setAttribute("onkeyup", "display(" + asciiInputCount + ", 'fileinput" + fileInputCount +"', 'fileoutput" + fileInputCount + "');");
        element.appendChild(input);
        
        var output = document.createElement("div");
        output.setAttribute("id", "fileoutput" + fileInputCount);
        output.setAttribute("class", "mathml");
        element.appendChild(output);
        
        fileinput.appendChild(element);
        fileInputCount++;
    }
    $(input).filestyle({ 
        image: "img/upload.png",
        imageheight : 22,
        imagewidth : 82,
        width : 118
    });
}

function fillASCIIInput(index, value) {
    var asciiinput = document.getElementById("asciiinput" + index);

    asciiinput.setAttribute("value", value);
}